package com.online.onlineshop.controllers;


import com.alibaba.fastjson.JSONObject;
import com.online.onlineshop.services.ForCartService;
import com.online.onlineshop.utils.ResultForFrontEnd;
import com.online.onlineshop.vo.CartValueObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController extends RootController {
    @Autowired
    ForCartService forCartService;

    /*
     * 加入购物车的控制方法
     *
     * */
    @RequestMapping("putToCart")
    public JSONObject putToCart(@RequestParam Integer pid, @RequestParam(defaultValue = "1") Integer amount, HttpSession session) {
        int uid = getUidFromSession(session);
        JSONObject jo = new JSONObject();
        if (uid != 0) {
            String userName = getUserNameFromSession(session);
            int cid = forCartService.putToCart(uid, pid, amount, userName);
            jo.put("stateCode", 200);
            jo.put("cid", cid);
        } else {
            jo.put("stateCode", 201);
        }
        return jo;
    }

    /*
     * 查询用户购物车数据的控制层方法
     *
     * */
    @RequestMapping({"", "/"})
    public ResultForFrontEnd<List<CartValueObject>> getCartVOByUid(HttpSession session) {
        int uid = getUidFromSession(session);
        List<CartValueObject> result = forCartService.getCartVOByUid(uid);
        return new ResultForFrontEnd<>(ok, result);
    }

    /*
     *
     * 查找勾选的购物车商品数据
     * */
    @RequestMapping("/findByCid")
    public ResultForFrontEnd<List<CartValueObject>> findByCid(String cids, HttpSession session) {
        int uid = getUidFromSession(session);
        List<CartValueObject> data = forCartService.findbycids(uid, cids);
        return new ResultForFrontEnd<>(ok, data);
    }

    @RequestMapping({"{cid}/num/add"})
    public ResultForFrontEnd<Integer> addNum(@PathVariable("cid") int cid, HttpSession session) {
        int uid = getUidFromSession(session);
        String userName = getUserNameFromSession(session);
        Integer rs = forCartService.addNum(cid, uid, userName);
        return new ResultForFrontEnd<>(ok, rs);

    }

    @RequestMapping({"{cid}/num/sub"})
    public ResultForFrontEnd<Integer> subNum(@PathVariable("cid") int cid, HttpSession session) {
        int uid = getUidFromSession(session);
        String userName = getUserNameFromSession(session);
        Integer rs = forCartService.subNum(cid, uid, userName);
        return new ResultForFrontEnd<>(ok, rs);
    }

    @RequestMapping("delete/{cid}")
    public ResultForFrontEnd<Integer> deleteByCid(@PathVariable("cid") int cid) {
        Integer rs = forCartService.deleteByCid(cid);
        return new ResultForFrontEnd<>(ok, rs);
    }

    @RequestMapping("deleteByCids")
    public ResultForFrontEnd<Integer> deleteByCids(Integer[] cids) {
        Integer rs = forCartService.deleteByCids(cids);
        return new ResultForFrontEnd<>(ok, rs);
    }

}
